package com.liuwei.msmall.getway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 在网关层统一进行跨域处理
 * @author llww
 * @date 2021/2/24 9:58
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource=new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");//允许任何请求头跨域
        corsConfiguration.addAllowedMethod("*");//允许任何请求方式跨域
        corsConfiguration.addAllowedOrigin("*");//允许任何来源跨域
        corsConfiguration.setAllowCredentials(true);//允许携带cook数据进行跨域
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        CorsWebFilter corsWebFilter = new CorsWebFilter(urlBasedCorsConfigurationSource);

        return corsWebFilter;
    }

}
